package com.wyh.domain.vo.groupOne.utils;

import java.util.ArrayList;
import java.util.List;

public class OrganizationTreeBuilder {

    public static List<MasterDataTreeNode> extractOrganizationTree(List<MasterDataTreeNode> originalTree) {
        List<MasterDataTreeNode> organizationTree = new ArrayList<>();
        for (MasterDataTreeNode node : originalTree) {
            MasterDataTreeNode orgNode = extractOrgNode(node);
            if (orgNode != null) {
                organizationTree.add(orgNode);
            }
        }
        return organizationTree;
    }

    private static MasterDataTreeNode extractOrgNode(MasterDataTreeNode node) {
        if (!"organization".equals(node.getType())) {
            return null;
        }

        // 创建新节点时保留 parentUniqueId
        MasterDataTreeNode newNode = new MasterDataTreeNode(node.getUnique_id(), node.getId(), node.getName(), node.getType(), node.getState());
        newNode.setParentUniqueId(node.getParentUniqueId());

        for (MasterDataTreeNode child : node.getChildren()) {
            MasterDataTreeNode childOrgNode = extractOrgNode(child);
            if (childOrgNode != null) {
                childOrgNode.setParentUniqueId(newNode.getUnique_id()); // 设置子节点的 parentUniqueId
                newNode.getChildren().add(childOrgNode);
            }
        }
        return newNode;
    }
}





